<template>
  <div class="wrapper">
    <div class="page-account">
      <div class="page-account-container">
        <div class="page-account-top">
          <div class="page-account-top-logo">
            <a target="_blank" href="https://element.eleme.cn">
              <img src="@/assets/logo/logo.svg">
            </a>
          </div>
          <div class="page-account-top-desc">
            Element UI 基于Vue2.0的网站快速成型组件库
          </div>
        </div>
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="off">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账户:admin">
              <template slot="prefix">
                <svg-icon class="login-icon" icon-class="kehuguanli" />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码:admin123" show-password @keyup.enter.native="handleLogin">
              <template slot="prefix">
                <svg-icon class="login-icon" icon-class="mima" />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="code">
            <el-row :gutter="30">
              <el-col :span="16">
                <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" @keyup.enter.native="handleLogin">
                  <template slot="prefix">
                    <svg-icon class="login-icon" icon-class="yanzheng" />
                  </template>
                </el-input>
              </el-col>
              <el-col :span="8" style="height:40px;">
                <img :src="codeUrl" style="height:40px;" @click="getCode">
              </el-col>
            </el-row>
          </el-form-item>
          <el-checkbox v-model="loginForm.rememberMe" class="remember-btn">自动登录</el-checkbox>
          <el-form-item>
            <el-button :loading="loading" class="submit-btn" @click.native.prevent="handleLogin">
              <span v-if="!loading">登录</span>
              <span v-else style="margin-left:5px;">登 录 中...</span>
            </el-button>
          </el-form-item>
          <el-row class="third-login">
            <span>其他登录方式</span>
            <svg-icon v-for="(item,key) in thirdLogin" :key="key" :icon-class="item" class="third-login-icon" />
          </el-row>
        </el-form>
      </div>
      <div class="copyright">
        <a target="_blank" href="http://www.ruoyi.vip">Copyright © 2021 Powered by Ruoyi & Element ui</a>
      </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from '@/api/login'
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt'
import { timeFix } from '@/utils/ruoyi'

export default {
  name: 'Login',
  data() {
    return {
      codeUrl: '',
      cookiePassword: '',
      loginForm: {
        username: '',
        password: '',
        rememberMe: false,
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '用户名不能为空' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '密码不能为空' }
        ],
        code: [{ required: true, trigger: 'change', message: '验证码不能为空' }]
      },
      thirdLogin: ['ding', 'wechat'],
      loading: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.codeUrl = 'data:image/gif;base64,' + res.img
        this.loginForm.uuid = res.uuid
      })
    },
    getCookie() {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set('username', this.loginForm.username, { expires: 30 })
            Cookies.set('password', encrypt(this.loginForm.password), {
              expires: 30
            })
            Cookies.set('rememberMe', this.loginForm.rememberMe, {
              expires: 30
            })
          } else {
            Cookies.remove('username')
            Cookies.remove('password')
            Cookies.remove('rememberMe')
          }
          this.$store
            .dispatch('Login', this.loginForm)
            .then(() => {
              this.$notify({
                title: '登录成功',
                message: `${timeFix()}，欢迎回来。`,
                type: 'success'
              })
              this.$router.push({ path: this.redirect || '/' }).catch(() => {})
            })
            .catch(() => {
              this.loading = false
              this.loginForm.code = ''
              this.getCode()
            })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import '~@/assets/styles/login.scss';

$login_theme: #409eff;
$font-size: 16px;

::v-deep .el-input__inner {
  height: 40px !important;
  padding-left: 38px !important;
  font-size: 16px !important;
}

::v-deep .el-button {
  background-color: $login_theme;
  border-color: $login_theme;
  color: #fff;
  font-size: $font-size !important;
  height: 38px !important;
}

::v-deep .el-checkbox__label {
  font-size: $font-size;
  color: #515a6e !important;
}

::v-deep .el-checkbox__inner {
  width: $font-size;
  height: $font-size;
  &::after {
    top: 2px !important;
    left: 5px !important;
  }
}

// 登录界面icon
.login-icon {
  font-size: 19px;
  vertical-align: -6px;
  margin-left: 5px;
}
</style>
